package com.ruoyi.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.domain.AdvMembersaddr;
import com.ruoyi.mapper.*;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.ruoyi.domain.AdvMembers;
import com.ruoyi.service.IAdvMembersService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
@Slf4j
public class AdvMembersServiceImpl implements IAdvMembersService
{
    @Autowired
    @Resource
    private AdvMembersMapper advMembersMapper;

    @Autowired
    @Resource
    private SysConfigMapper sysConfigMapper;

    @Autowired
    @Resource
    private AdvInoutstockMapper advInoutstockMapper;

    @Autowired
    @Resource
    private AdvDevicerelationMapper advDevicerelationMapper;

    @Autowired
    @Resource
    private AdvReservbroadcastMapper advReservbroadcastMapper;

    @Autowired
    @Resource
    private AdvCollectMapper advCollectMapper;

    @Autowired
    @Resource
    private AdvOrdersdetailMapper advOrdersdetailMapper;

    @Autowired
    @Resource
    private AdvOrdersMapper advOrdersMapper;

    @Autowired
    @Resource
    private AdvMembersmsgMapper advMembersmsgMapper;

    @Autowired
    @Resource
    private AdvInfoliksMapper advInfoliksMapper;

    @Autowired
    @Resource
    private AdvIncomedetailMapper advIncomedetailMapper;

    @Autowired
    @Resource
    private AdvIncomeMapper advIncomeMapper;

    @Autowired
    @Resource
    private AdvProductsMapper advProductsMapper;

    @Autowired
    @Resource
    private AdvBroadcastMapper advBroadcastMapper;



    /**
     * 查询
     *
     * @param advMembersId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AdvMembers selectAdvMembersByAdvMembersId(Long advMembersId)
    {
        return advMembersMapper.selectAdvMembersByAdvMembersId(advMembersId);
    }

    /**
     * 查询列表(带下级会员数据)
     *
     * @param advMembers 【带下级会员数据，旧的，后续可能会废弃】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AdvMembers> selectAdvMembersListSecond(AdvMembers advMembers) {
        System.out.println(advMembers);
        List<AdvMembers> members = advMembersMapper.selectAdvMembersList(advMembers);
        for (AdvMembers member : members) {
            final List<AdvMembers> childred =
                    advMembersMapper.selectSecondMembers(member.getAdvMembersId());
            member.setChildren(childred);
        }
        return members;
    }

    /**
     * 查询列表(带下级会员数据)
     *
     * @param advMembers 【带下级会员数据，关联上级会员，最新】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AdvMembers> selectAdvMembersListSecondForp(AdvMembers advMembers) {
        System.out.println(advMembers);
        List<AdvMembers> members = advMembersMapper.selectAdvMembersListForp(advMembers);
        for (AdvMembers member : members) {
            final List<AdvMembers> childred =
                    advMembersMapper.selectSecondMembers(member.getAdvMembersId());
            member.setChildren(childred);
        }
        return members;
    }

    /**
     * 查询列表
     *
     * @param advMembers 【关联上级会员表，旧的，后续可能会废弃】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AdvMembers> selectAdvMembersList(AdvMembers advMembers)
    {
        return advMembersMapper.selectAdvMembersList(advMembers);
    }

    /**
     * 查询列表
     *
     * @param advMembers 【关联上级会员表，最新】
     * @return 【请填写功能名称】
     */

    @Override
    public List<AdvMembers> selectAdvMembersListForp(AdvMembers advMembers) {
        return advMembersMapper.selectAdvMembersListForp(advMembers);
    }

    /**
     * 新增
     *
     * @param advMembers
     * @return 结果
     */
    @Override
    public int insertAdvMembers(AdvMembers advMembers)
    {
        String id = (System.currentTimeMillis() + "").substring(4);
        advMembers.setAdvMembersId(Long.valueOf(id));
        return advMembersMapper.insertAdvMembers(advMembers);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param advMembers 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAdvMembers(AdvMembers advMembers)
    {
        return advMembersMapper.updateAdvMembers(advMembers);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param advMembersIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteAdvMembersByAdvMembersIds(Long[] advMembersIds)
    {
        try{
//            for(Long id : advMembersIds) {
//                // 拿到id判断是否有下级会员
//                final Integer i = advMembersMapper.checkNextById(id);
//                if (i != null && i > 0) {
//                    // 表示有下级会员
//                    return true;
//                }
//            }
            advMembersMapper.deleteMemberByAddr(advMembersIds);
            advMembersMapper.deleteMemberByMsg(advMembersIds);
            advMembersMapper.deleteMemberByInfoliks(advMembersIds);
            advMembersMapper.deleteMemberByReservbroadcast(advMembersIds);
            advMembersMapper.deleteMemberByCollect(advMembersIds);
            advMembersMapper.deleteMemberByOrdersdetail(advMembersIds);
            advMembersMapper.deleteMemberByIncomedetail(advMembersIds);
            advMembersMapper.deleteMemberByIncome(advMembersIds);
            advMembersMapper.deleteMemberByOrders(advMembersIds);
            final int i = advMembersMapper.deleteAdvMembersByAdvMembersIds(advMembersIds);
            return true;
        }catch (MyBatisSystemException e){
            throw new RuntimeException("有关联业务数据，请先处理关联数据再删除");
        }

    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param advMembersId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdvMembersByAdvMembersId(Long advMembersId)
    {
        try {
            return advMembersMapper.deleteAdvMembersByAdvMembersId(advMembersId);
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("有关联业务数据，请先处理关联数据再删除");
        }

    }

    // 小程序登录
    @Override
    public AdvMembers appLogin(AdvMembers advMembers) {
        log.debug("用户进行了登录:" + advMembers);
        if(advMembers.getWeOpenid().equals("")){
            return advMembers;
        }
        /*
         *  根据 weOpenid 查询用户
         *  有数据: 表示已经注册过了, 就直接返回查询到的数据
         *  没有数据: 表示没有注册, 就往会员表中添加一个数据
         */
        AdvMembers pramas = new AdvMembers();
        pramas.setWeOpenid(advMembers.getWeOpenid());
        AdvMembers advMembersBySelect = advMembersMapper.selectByWeOpenID(advMembers.getWeOpenid());
        if(!Objects.isNull(advMembersBySelect)) {
            if(advMembersBySelect.getMemberStatus().equals("0")) {
                // 如果被拉黑了就返回null
                return null;
            }
            return advMembersBySelect;
        }
//        List<AdvMembers> membersList = advMembersMapper.selectAdvMembersList(pramas);
//        if (membersList != null && membersList.size() > 0) {
//            AdvMembers advMembersUser = membersList.get(0);
//            if(advMembersUser.getMemberStatus().equals("0")) {
//                // 如果被拉黑了就返回null
//                return null;
//            }
//            return membersList.get(0);
//        }


        System.out.println("新用户注册: " + advMembers.getWeOpenid());
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey("sys_payrate");
        SysConfig configResult = sysConfigMapper.selectConfig(sysConfig);
        //advMembers.setIsBroadcast(1L);
        advMembers.setIsBroadcast("0");
        advMembers.setMemberStatus("1");
        advMembers.setIsSaler("0");
        advMembers.setPaySpecmode("0");
        advMembers.setPayRate(Integer.valueOf(configResult.getConfigValue()));
        advMembers.setJoinDate(DateUtil.getDate_YYYY_MM_dd_HH_mm_ss());
        int i = advMembersMapper.insertAdvMembers(advMembers);
        return advMembers;
    }

    /**
     * 查询主播
     * @return
     */
    @Override
    public List<AdvMembers> selectAnchor() {
       return advMembersMapper.selectAnchor();
    }

    @Override
    public int resetRate(AdvMembers advMembers) {
        int rows = advMembersMapper.resetRate(advMembers);
        return rows;
    }

    @Transactional
    @Override
    public boolean initAll() {
        try {
            advInoutstockMapper.clear();
            advDevicerelationMapper.clear();
            advReservbroadcastMapper.clear();
            advCollectMapper.clear();
            advOrdersdetailMapper.clear();
            advMembersmsgMapper.clear();
            advInfoliksMapper.clear();
            advIncomedetailMapper.clear();
            advIncomeMapper.clear();
            advOrdersMapper.clear();
            advProductsMapper.clear();
            advBroadcastMapper.clear();
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Transactional
    @Override
    public boolean initOne(AdvMembers advMembers) {
        try {
            advInoutstockMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advDevicerelationMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advReservbroadcastMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advCollectMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advOrdersdetailMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advMembersmsgMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advInfoliksMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advIncomedetailMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advIncomeMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advOrdersMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
            advBroadcastMapper.deleteByAdvMembersId(String.valueOf(advMembers.getAdvMembersId()));
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
